package binary_tree.leetcode.medium;

import java.util.TreeSet;

/**
 * @author bruin_du
 * @description 值和下标之差都在给定的范围内
 * @date 2022/8/16 20:43
 **/
public class OfferII057_ContainsNearbyAlmostDuplicate {
    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        TreeSet<Long> set = new TreeSet<>();
        for (int i = 0; i < nums.length; i++) {
            Long ceiling = set.ceiling((long)nums[i] - (long)t);
            if (ceiling != null && ceiling <= ((long)nums[i] + (long)t)) {
                return true;
            }
            set.add((long)nums[i]);
            if (i >= k) set.remove((long)nums[i - k]);
        }
        return false;
    }
}
